﻿const utils = {
    searchTags: (options = {}) => {

        const itemHeight = options.itemHeight || 34;
        const elem = options.elem;
        const data = options.data;

        const nodes = data.map(item => {
            let url = new URL(location.href);
            let urlType = url.searchParams.get(item.value) || '';
            let nodes = `
                    <div class="search-item collapse">
                        <span class="label item">${item.name}：</span>
                        <div class="filter-items">
                           ${item.children.map(child => `<a class="item ${urlType === child.value ? 'active' : ''}" name="${item.value}" value="${child.value}">${child.name}</a>`).join('')}
                        </div>
                        <a class="item collapse" style="display:none;">
                            展开
                        </a>
                    </div>
                `
            return nodes;
        }).join('')

        $(elem).append(nodes)

        $(elem).find('.search-item').each((index, item) => {
            let flag = false;
            let jNode = $(item)
            let height = jNode.height();
            if (height > itemHeight) {
                jNode.height(itemHeight);
                let collapseNode = jNode.find('.collapse');
                collapseNode.show()
                collapseNode.on('click', item => {
                    if (!flag) {
                        collapseNode.text('收起')
                        jNode.height(height);
                    } else {
                        collapseNode.text('展开')
                        jNode.height(itemHeight);
                    }
                    flag = !flag
                })
            }
        })

        $('.filter-items .item').on('click', event => {
            let jNode = $(event.target)
            options.click && typeof options.click === 'function' && options.click({ name: jNode.attr('name'), value: jNode.attr('value') })
        })

    },
    timeFormat: (timestamp) => {
        var mistiming = Math.round((Date.now() - timestamp) / 1000);
        var arrr = ['年', '个月', '周', '天', '小时', '分钟', '秒'];
        var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
        for (var i = 0; i < arrn.length; i++) {
            var inm = Math.floor(mistiming / arrn[i]);
            if (inm != 0) {
                return inm + arrr[i] + '前';
            }
        }
    },
    getQueryVariable: (variable) => {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    },
    debounce: (fun, delay) => {
        let timer = null
        return (...rest) => {
            clearTimeout(timer)
            timer = setTimeout(() => {
                fun.apply(this, rest)
            }, delay)
        }
    }
}